package com.lovo.sgproj.frame.roommanage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoDate;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.sgproj.Config;
import com.lovo.sgproj.bean.RecordBean;
import com.lovo.sgproj.frame.MainFrame;
import com.lovo.sgproj.service.RecordService;
import com.lovo.sgproj.service.RoomService;
import com.lovo.sgproj.service.imp.RecordServiceImp;
import com.lovo.sgproj.service.imp.RoomServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class RoomEqpDialog extends JDialog {

    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

    private LovoTable recordTab;

    private LovoDate reportDate;// 报损日期
    private JLabel descriptLab;
    private JTextArea descriptArea;

    private LovoButton addRecordBtn;
    private LovoButton processBtn;
    private LovoButton exitBtn;

    private MainFrame frame;
    private int roomId;

    public RoomEqpDialog(MainFrame frame, int roomId) {
        // TODO Auto-generated constructor stub
        super(frame, "房间设施维护", true);

        this.frame = frame;
        this.roomId = roomId;

        this.setBounds(350, 250, 420, 400);
        this.setLayout(null);
        this.initTable();
        this.init();
        this.initBtn();

        this.setVisible(true);
    }

    private void initTable() {
        this.recordTab = new LovoTable(this, new String[]{"损坏描述", "报损日期",
                "是否解决"}, new String[]{"description", "recordDate", "solution"},// 数组中应该是对应属性的属性名
                "recordID");// 填入唯一标示属性
        this.recordTab.setSizeAndLocation(20, 10, 360, 150);

        RoomService roomService = context.getBean(RoomServiceImp.class);
        //System.out.println(this.roomId);
        ArrayList<RecordBean> recordLst = roomService.showRecordsByRoomID(this.roomId);
        addRecordToTab(recordLst);
    }

    public void addRecordToTab(ArrayList<RecordBean> recordLst) {
        this.recordTab.updateLovoTable(recordLst);
        for (int i = 0; i < recordLst.size(); i++) {
            this.recordTab.setValueAt(recordLst.get(i).getSolution() ? "已解决" : "未解决", i, 2);
        }
    }

    private void init() {
        // TODO Auto-generated method stub
        this.reportDate = new LovoDate("报损日期", 80, 180, this);

        this.descriptLab = new JLabel("损坏描述");
        this.descriptLab.setBounds(80, 220, 60, 30);
        this.add(this.descriptLab);

        this.descriptArea = new JTextArea();
        JScrollPane jsp = new JScrollPane(this.descriptArea);
        jsp.setBounds(150, 220, 220, 100);
        this.add(jsp);
    }

    private void initBtn() {
        // TODO Auto-generated method stub
        this.addRecordBtn = new LovoButton("添加设施损坏记录", 10, 330, this);
        this.addRecordBtn.setSize(120, 20);

        this.processBtn = new LovoButton("处理已损坏设备", 145, 330, this);
        this.processBtn.setSize(120, 20);

        this.exitBtn = new LovoButton("退出", 280, 330, this);
        this.exitBtn.setSize(120, 20);

        this.addRecordBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                RecordBean record = new RecordBean();
                record.setRecordDate(reportDate.getDate());
                record.setDescription(descriptArea.getText());

                RecordService recordService = context.getBean(RecordServiceImp.class);
                recordService.addRecord(record, roomId);

                frame.getRoomNode().addListener();

                RoomEqpDialog.this.dispose();
            }
        });
        this.processBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                int recordid = RoomEqpDialog.this.recordTab.getKey();
                //System.out.println(recordid);
                RecordService recordService = context.getBean(RecordServiceImp.class);
                recordService.processRecord(recordid, roomId);
                frame.getRoomNode().addListener();

                RoomEqpDialog.this.dispose();
            }
        });
        this.exitBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                RoomEqpDialog.this.dispose();
            }
        });
    }

}
